package com.daocao.jy.constant;

import android.text.TextUtils;

import com.daocao.jy.Application;
import com.daocao.jy.api.model.InitAppModel;
import com.daocao.jy.api.model.LoginModel;
import com.daocao.jy.api.model.UserInfoModel;
import com.daocao.jy.event.LoginSuccessEvent;
import com.daocao.jy.event.LogoutEvent;
import com.daocao.jy.util.DateTime;
import com.daocao.jy.util.PreferenceUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by xsooy-pc on 18-3-7.
 */

public class UserInfoGlobal {
    public static void saveInitData(InitAppModel.DataBean item) {
        PreferenceUtils.putString(Application.getInstance(), "is_login", item.is_logined);
        PreferenceUtils.putString(Application.getInstance(), "lastest_version", item.version.getLastest_version());
        PreferenceUtils.putString(Application.getInstance(), "download_url", item.version.getDownload_url());
        PreferenceUtils.putString(Application.getInstance(), "upgrade_msg", item.version.getUpgrade_msg());

        PreferenceUtils.putString(Application.getInstance(), "is_trueshop", item.is_trueshop);
    }


    public static void saveLoginInfo(LoginModel.DatasBean item) {
        PreferenceUtils.putString(Application.getInstance(), "email", item.email);
        PreferenceUtils.putString(Application.getInstance(), "icon", item.icon);
        PreferenceUtils.putString(Application.getInstance(), "phone", item.phone);
        PreferenceUtils.putString(Application.getInstance(), "username", item.username);
        login();
    }

    public static void saveUserInfo(UserInfoModel.DatasBean item) {
        PreferenceUtils.putString(Application.getInstance(), "icon", item.icon);
        PreferenceUtils.putString(Application.getInstance(), "phone", item.phone);
        PreferenceUtils.putString(Application.getInstance(), "username", item.username);
        PreferenceUtils.putString(Application.getInstance(), "birthday", item.birthday);
        PreferenceUtils.putString(Application.getInstance(), "sex", item.sex);
        PreferenceUtils.putInt(Application.getInstance(), "level", item.level);
    }

    public static String getUserSex() {
        if (PreferenceUtils.getString(Application.getInstance(), "sex", "").equals("1")) {
            return "男";
        } else if (PreferenceUtils.getString(Application.getInstance(), "sex", "").equals("2")) {
            return "女";
        } else return "未设置";
    }

    public static String getUserBirth() {
        if (TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "birthday", ""))) {
            return "未设置";
        } else {
            return DateTime.toDate(Long.valueOf(PreferenceUtils.getString(Application.getInstance(), "birthday", "")) * 1000);
        }
    }

    public static String getUserPhone() {
        return PreferenceUtils.getString(Application.getInstance(), "phone", "");
    }

    public static int getUserLevel() {
        return PreferenceUtils.getInt(Application.getInstance(), "level", 1);
    }

    public static String getUserNick() {
        if (!TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "username", ""))) {
            return PreferenceUtils.getString(Application.getInstance(), "username", "");
        } else {
            return PreferenceUtils.getString(Application.getInstance(), "phone", "");
        }
    }

    public static String getUserIcon() {
        if (!TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "icon", ""))) {
            return PreferenceUtils.getString(Application.getInstance(), "icon", "");
        } else {
            return "";
        }
    }


    public static void saveNickName(String name) {
        PreferenceUtils.putString(Application.getInstance(), "nickname", name);
    }

    public static void saveUserSex(String sex) {
        PreferenceUtils.putString(Application.getInstance(), "sex", sex);
    }

    public static boolean isLogin() {
        if (PreferenceUtils.getString(Application.getInstance(), "is_login", "0").equals("1")) {
            return true;
        } else {
            return false;
        }
    }

    public static void login() {
        PreferenceUtils.putString(Application.getInstance(), "is_login", "1");
        EventBus.getDefault().post(new LoginSuccessEvent());
    }

    public static void logout() {
        PreferenceUtils.putString(Application.getInstance(), "token", "");
        PreferenceUtils.putString(Application.getInstance(), "is_login", "");
        EventBus.getDefault().post(new LogoutEvent());
    }


}
